package ebj.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;

public class 年龄计算器 {
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void test() throws ParseException {
		Date 出生日期 = simpleDateFormat.parse("2016-07-14 18:38:00");
		System.out.println("当前年龄为： " + getAge(出生日期, new Date()));
	}
	   
	private String getAge(Date 出生时间, Date 当前时间) {
	    Calendar calendar_birth = Calendar.getInstance();
	    calendar_birth.setTime(出生时间);
		Calendar calendar_current = Calendar.getInstance();
		calendar_current.setTime(当前时间);
		int year_birth = calendar_birth.get(Calendar.YEAR);
		int year_current = calendar_current.get(Calendar.YEAR);
		
		int month_birth = calendar_birth.get(Calendar.MONTH);
		int month_current = calendar_current.get(Calendar.MONTH);
		
		int day_birth = calendar_birth.get(Calendar.DATE);
		int day_current = calendar_current.get(Calendar.DATE);
		
		return (year_current - year_birth) + "岁, 零" + (month_current - month_birth) + "个月, "
		        + (day_current - day_birth) + "天";
//		return simpleDateFormat.format(calendar.getTime());
	}
}